﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.Common;
using Mimoza.DBLayer.Transport;
using Mimoza.Transport;
using Mimoza.DBLayer.Transport.Client;

namespace Mimoza.DBLayer.Transport.Server
{
    public class PoliciesRepository :
        ServerRepository<PolicyRecord, PoliciesRepositoryCustomRequest, IDBPolicyRepository>,
        IDBPolicyRepository
    {
        public PoliciesRepository(IDBPolicyRepository dbPolicies,
            CollectionManager collectionManager, ConnectionList connList)
            : base(dbPolicies, collectionManager, connList)
        {
            PoliciesRepositoryTranferRequest.RegisterInTransport();
            PoliciesRepositoryTranferRequest.Repository = this;
        }

        public PolicyRecord GetPolicy(Guid ID)
        {
            return BaseRepository.GetPolicy(ID);
        }

        public IEnumerable<PolicyRecord> GetPolicies(Guid containerID)
        {
            return BaseRepository.GetPolicies(containerID);
        }

        override public void Insert(PolicyRecord Policy)
        {
            BaseRepository.Insert(Policy);
        }

        override public void Update(PolicyRecord Policy)
        {
            BaseRepository.Update(Policy);
        }

        override public void Delete(PolicyRecord Policy)
        {
            BaseRepository.Delete(Policy);
        }

        public override TransferObject ProcessRequest(Connection conn, PoliciesRepositoryCustomRequest request)
        {
            TransferObject reply = null;
            switch (request.OperationType)
            {
                case PoliciesRepositoryCustomRequest.CustomOperationType.GetPolicy:
                    reply = GetPolicy(request);
                    break;
                case PoliciesRepositoryCustomRequest.CustomOperationType.GetPolicies:
                    reply = GetPolicies(conn,request);
                    break;
            }

            return reply;
        }

        protected TransferObject GetPolicy(PoliciesRepositoryCustomRequest request)
        {
            PolicyRecord record = GetPolicy(new Guid(request.Argument1));

            return new RepositoryReply<PolicyRecord>(record);
        }

        protected TransferObject GetPolicies(Connection conn, PoliciesRepositoryCustomRequest request)
        {
            IEnumerable<PolicyRecord> enumerator = GetPolicies(new Guid(request.Argument1));

            return RegisterCollection(enumerator, conn);
        }
    }
}
